<?php

/**
 * Description of Model_user
 *
 * @author Meta Sanjaya
 */
class Model_user extends MY_Model {

    public function __construct() {
        parent::__construct();
    }

    /**
     *
     * @param string $user
     * @param string $pass
     * @return string
     */
    private function _get_password($user, $pass) {
        return md5($pass . ':' . $user);
    }

    /**
     *
     * @param string $user
     * @param string $pass
     * @return int
     */
    function check($user, $pass) {
        $pass = $this->_get_password($user, $pass);
        $query = $this->db->query("select * from `" . DATABASE_ERP . "`.`user` where `username` = '$user' and `password` = '$pass' and `aktif` = 1");

        if ($query->num_rows() < 1) {
            return false;
        }

        return $query->row()->id;
    }

    /**
     *
     * @param int $id
     * @return Obj_User
     */
    function get_user($id) {
        $query = $this->db->query("select * from `" . DATABASE_ERP . "`.`user` where `id` = $id");

        if ($query->num_rows() < 1) {
            return false;
        }

        $user = $query->row();
        $model_group = $this->load_model('model_group');
        $user->group = $model_group->get_group($user->group);

        return $user;
    }

    /**
     *
     * @param string $user
     * @param string $pass
     * @param int $group
     * @return int
     */
    function add_user($user, $pass, $group) {
        $pass = $this->_get_password($user, $pass);
        $this->db->query("insert into `" . DATABASE_ERP . "`.`user`(`username`,`password`) values('$username', '$pass', $group)");
        return $this->db->insert_id();
    }

    /**
     *
     * @param int $id
     * @return boolean
     */
    function delete_user($id) {
        return $this->db->simple_query("delete from `" . DATABASE_ERP . "`.`user` where `id` = $id");
    }

}

?>
